﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using VSS_AHT.DataAccess;
using VSSAHT.DataAccess;
using VSSAHT.Transfer;
using VSS_AHT.Forms;
using VSS_AHT.ReportForms;
using System.IO;
namespace VSS_AHT
{
    public partial class FormMain : DevExpress.XtraBars.Ribbon.RibbonForm
    {

        private static volatile FormMain instance;
        private static object syncRoot = new Object();
        private bool _isCorrectConnection = false;
        public static FormMain Instance
        {
            get
            {

                if (instance == null)
                {
                    instance = new FormMain();
                }

                return instance;
            }
        }

        #region Ctor
        public FormMain()
        {
            InitializeComponent();
            IConnection dataAccess = new Connection();
            if (SystemInformation.Network)
            {
                this.Initialize();
            }
            else
            {
                MessageBox.Show("Kết nối mạng không được tìm thấy. Kiểm tra lại", "Chú ý", MessageBoxButtons.OK);
            }
            //Initialize();
        }
        #endregion

        #region Methods

        public void Initialize()
        {
            this.Size = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height - 30);
            this.Location = new Point(0, 0);


           // if (!Directory.Exists(Common.storagePath))
           // {
                //Directory.CreateDirectory(Common.storagePath);
            if (!Directory.Exists(Common.storagePath + "/0"))
            {
                Common.ShowAlert("Không tìm thấy đường dẫn: " + Common.storagePath + "  , để lưu Excel phiếu cân. Xin cấu hình lại đường dẫn trước khi cân.");
            }
            else if (!Directory.Exists(Common.storagePath + "/1"))
                Common.ShowAlert("Không tìm thấy đường dẫn: " + Common.storagePath + "  , để lưu Excel phiếu cân. Xin cấu hình lại đường dẫn trước khi cân.");
            //}
           // else
            //{
            //    Common.ShowAlert("Đường dẫn : " + Common.storagePath + " không tìm thấy. Bạn hãy thay đổi đường dẫn lưu Excel phiếu cân");

            //}
            
            this.ribbon.ItemClick += new ItemClickEventHandler(ribbon_ItemClick);
        }

        protected void ribbon_ItemClick(object sender, ItemClickEventArgs e)
        {
            switch (e.Item.Name)
            {
                case "ribbonButtonReport":
                    FormReport.Instance.MdiParent = this;
                    FormReport.Instance.Show();
                    FormReport.Instance.WindowState = FormWindowState.Maximized;
                    break;
                case "ribbonButtonScaleTicket":
                    FormScaleTicket.Instance.MdiParent = this;
                    FormScaleTicket.Instance.Show();
                    FormScaleTicket.Instance.WindowState = FormWindowState.Maximized;
                    break;
                case "ribbonButtonScaleTicketList":
                    FormScaleTicketList.Instance.MdiParent = this;
                    FormScaleTicketList.Instance.Show();
                    FormScaleTicketList.Instance.WindowState = FormWindowState.Maximized;
                    break;
                case "barButtonCustomer":
                    FormCustomer.Instance.MdiParent = this;
                    FormCustomer.Instance.Show();
                    FormCustomer.Instance.WindowState = FormWindowState.Maximized;
                    break;
                case "barButtonConfigCompany":
                    FormCompanyInfor.Instance.MdiParent = this;
                    FormCompanyInfor.Instance.Show();
                    FormCompanyInfor.Instance.WindowState = FormWindowState.Maximized;
                    break;
                case "barButtonConfigComPort":
                    FormConfigComPort.Instance.MdiParent = this;
                    FormConfigComPort.Instance.Show();
                    FormConfigComPort.Instance.WindowState = FormWindowState.Maximized;
                    break;
                case "barButtonConfigPrinter":
                    FormPrinter.Instance.MdiParent = this;
                    FormPrinter.Instance.Show();
                    FormPrinter.Instance.WindowState = FormWindowState.Maximized;
                    //FormConfigPrinter form1 = new FormConfigPrinter();

                    //form1.MdiParent = this;
                    //form1.Show();
                    break;
                case "barButtonItem":
                    FormItems.Instance.MdiParent = this;
                    FormItems.Instance.Show();
                    FormItems.Instance.WindowState = FormWindowState.Maximized;
                    break;
                case "barButtonLocation":
                    FormLocation.Instance.MdiParent = this;
                    FormLocation.Instance.Show();
                    FormLocation.Instance.WindowState = FormWindowState.Maximized;
                    break;
                case "barButtonRequireNumber":
                    FormRequireNumber.Instance.MdiParent = this;
                    FormRequireNumber.Instance.Show();
                    FormRequireNumber.Instance.WindowState = FormWindowState.Maximized;
                    break;
                case "barButtonModelVehicle":
                    FormModelVehicle.Instance.MdiParent = this;
                    FormModelVehicle.Instance.Show();
                    FormModelVehicle.Instance.WindowState = FormWindowState.Maximized;
                    break;
                case "barButtonUser":
                    FormUser.Instance.MdiParent = this;
                    FormUser.Instance.Show();
                    FormUser.Instance.WindowState = FormWindowState.Maximized;
                    break;
                case "barButtonRole":
                    FormRoles.Instance.MdiParent = this;
                    FormRoles.Instance.Show();
                    FormRoles.Instance.WindowState = FormWindowState.Maximized;
                    break;
            }
        }
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
        }
        #endregion

        #region Events
        protected override void OnLoad(EventArgs e)
        {
            //if (_isCorrectConnection)
            //{
            // Correct connection
            FormLogin login = new FormLogin();
            if (login.ShowDialog() != DialogResult.OK)
            {
                this.Close();
                return;
            }

            // Permission
            if (Common.UserLogin.Role != null && Common.UserLogin.Role.Permitted != null)
            {
                bool b = (Common.UserLogin.Role.Permitted.CustomerPermitted & 1) == 1;
                this.barButtonCustomer.Visibility = b ? BarItemVisibility.Always : BarItemVisibility.Never;
                this.ribbonPageGroup5.Visible = b;
                b = (Common.UserLogin.Role.Permitted.ItemPermitted & 1) == 1;
                this.barButtonItem.Visibility = b ? BarItemVisibility.Always : BarItemVisibility.Never;
                this.ribbonPageGroup5.Visible = b;

                b = (Common.UserLogin.Role.Permitted.LocationPermitted & 1) == 1;
                this.ribbonPageGroup9.Visible = b;
                b = (Common.UserLogin.Role.Permitted.ModelVehiclePermitted & 1) == 1;
                this.barButtonModelVehicle.Visibility = b ? BarItemVisibility.Always : BarItemVisibility.Never;
                this.ribbonPageGroup10.Visible = b;

                b = (Common.UserLogin.Role.Permitted.RequireNumberPermitted & 1) == 1;
                this.barButtonRequireNumber.Visibility = b ? BarItemVisibility.Always : BarItemVisibility.Never;
                this.ribbonPageGroup10.Visible = b;
                b = (Common.UserLogin.Role.Permitted.UserPermitted & 1) == 1;
                this.ribbonPageGroup11.Visible = b;
                b = (Common.UserLogin.Role.Permitted.ScaleTicketPermitted & 1) == 1;
                this.ribbonGroupScaleTicket.Visible = b;

                b = (Common.UserLogin.Role.Permitted.ConfigPermitted & 1) == 1;
                this.ribbonPage1.Visible = b;
            }

            this.ribbonPageGroup12.Visible = false;
            this.ribbonButtonStatus.Caption = Common.UserLogin.Username;
            this.ribbonButtonStatus.Strings.Add("IP Address: " + Common.IPAddress);

            // Init port to scale vehicles
            Common.InitPort();

            base.OnLoad(e);
        }
        #endregion


    }
}